package com.jyw.business.obstacles.domain.vo.wechat;

import com.jyw.business.obstacles.domain.vo.web.HomeMineServerVO;
import com.jyw.common.annotation.Conversion;
import com.jyw.common.enums.ConversionType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;

import java.util.List;
import java.util.stream.Collectors;

@ApiModel("微信工单日报统计实体")
@Data
@NoArgsConstructor
@FieldNameConstants
public class WxOrderDailyVO {

    @ApiModelProperty("人员Id")
    @Conversion(value = ConversionType.USER,name = Fields.userName)
    private String userId;

    @ApiModelProperty("人员名称")
    private String userName;

    @ApiModelProperty("工单计数")
    private List<HomeMineServerVO> orderCount;

    public WxOrderDailyVO(List<HomeMineServerVO> vos) {
        //处理人员名称
        String userId = vos
                .stream()
                .map(data -> data.getUserId().toString())
                //去重
                .distinct()
                .collect(Collectors.joining(","));
        setUserId(userId);
        //根据类型分类汇总
        List<HomeMineServerVO> orderCount = vos
                .stream()
                //分组
                .collect(Collectors.groupingBy(HomeMineServerVO::getType))
                .entrySet()
                .stream()
                //汇总
                .map(data -> {
                    HomeMineServerVO vo = new HomeMineServerVO();
                    vo.setType(data.getKey());
                    vo.setCount(data.getValue().stream().mapToInt(HomeMineServerVO::getCount).sum());
                    return vo;
                }).collect(Collectors.toList());
        setOrderCount(orderCount);
    }
}
